home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 30
/
Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso
/
Aminet
/
text
/
misc
/
RenameFont.lha
/
RenameFont.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-01-30
|
5KB
|
236 lines
/*
** RenameFonts.c
*/
/// Init
#include <exec/alerts.h>
#include <exec/exec.h>
#include <proto/exec.h>
#include <dos/dos.h>
#include <dos/rdargs.h>
#include <proto/dos.h>
#include <string.h>
#define BUFSIZE 8*1024
#define TTF 0x00010000L
#define AFM 0x53746172L
STRPTR TEMPLATE = "FILE/A";
STRPTR PROG_NAME = "RenameFont";
STRPTR VERSION = "$VER: RenameFont 1.0 (29.1.99) by Marco Amadori";
/*
$Compile: math=standard NOSTACKCHECK DATA=FARONLY STRSECT=CODE DEFINE=_USEOLDEXEC_
$Link: LIBRARY lib:amiga.lib LIB:SC.lib SC SD ND
$HISTORY:
29 Jan 1999 : 1.0 : First Version
*/
STRPTR PFB = "%!";
UBYTE PFBPOS = 6;
void ChangeName( STRPTR , STRPTR, LONG );
void ChangeNamePFB( STRPTR , STRPTR , LONG );
void ChangeNameAMF( STRPTR , STRPTR , LONG );
void ChangeNameTTF( STRPTR , STRPTR , LONG );
STRPTR FindString( STRPTR , STRPTR , STRPTR , ULONG );
void RenameFont( STRPTR , STRPTR );
struct DosLibrary *DOSBase;
///
/// LONG main (void)
LONG main (void)
{
struct {
STRPTR FileName;
} args = {0};
struct RDArgs *rda;
LONG rc = RETURN_FAIL;
STRPTR buffer;
if( DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37))
{
if( rda = (struct RDArgs *) AllocDosObject(DOS_RDARGS, 0))
{
rda->RDA_ExtHelp =
"\n"
"FILE/A: Font to rename\n "
" It Recognizes: PFB,AMF \n";
rda->RDA_Source.CS_Buffer = 0;
rda->RDA_Source.CS_Length = 0;
if(ReadArgs( TEMPLATE, (LONG *) &args, rda))
{
if( buffer = AllocVec( BUFSIZE, MEMF_CLEAR))
{
ChangeName( args.FileName, buffer, BUFSIZE);
rc = RETURN_OK;
FreeVec( buffer);
} else Alert(AT_Recovery | AG_NoMemory | AO_ExecLib);
FreeArgs(rda);
} else PrintFault(IoErr(), PROG_NAME);
FreeDosObject( DOS_RDARGS, (APTR) rda);
} else Alert(AT_Recovery | AG_NoMemory | AO_ExecLib);
CloseLibrary ((struct Library *) DOSBase);
} else Alert(AT_Recovery | AG_OpenLib | AO_DOSLib);
return rc;
}
///
/// void ChangeName(STRPTR filename, STRPTR buffer, LONG dim)
void ChangeName(STRPTR filename, STRPTR buffer, LONG dim)
{
BPTR filelock;
STRPTR s,r;
if( filelock = Open( filename, MODE_OLDFILE))
{
if( Read( filelock, (void *) buffer, dim))
{
switch ( *( (LONG *) buffer) )
{
case AFM:
ChangeNameAMF(filename, (STRPTR) buffer, dim);
break;
case TTF:
ChangeNameTTF(filename, (STRPTR) buffer, dim);
break;
default:
for( s = buffer+PFBPOS, r = PFB; *r == *s ; r++, s++);
if(*r == 0) ChangeNamePFB(filename, (STRPTR) buffer, dim);
else Printf( "Unknow Font Type: %s\n", filename);
}
} else PrintFault(IoErr(),filename);
Close(filelock);
}
}
///
/// void ChangeNamePFB(BPTR filelock, STRPTR buffer, LONG BufDim)
void ChangeNamePFB(STRPTR oldfilename, STRPTR buffer, LONG BufDim)
{
STRPTR bra="FontName /",ket=" def";
STRPTR ext=".PFB";
STRPTR filename,s,r;
if( filename = FindString( buffer, bra, ket, BufDim))
{
for( s = filename; *s ; s++);
for( r = ext ; *r; *s++ = *r++);
*s = 0;
Printf("Rename %s as %s\n", oldfilename, filename);
RenameFont( oldfilename, filename);
}
}
///
/// void ChangeNameAMF(STRPTR oldfilename, STRPTR buffer, LONG BufDim)
void ChangeNameAMF(STRPTR oldfilename, STRPTR buffer, LONG BufDim)
{
STRPTR bra="FontName ";
STRPTR ext=".AFM";
STRPTR filename,s,r;
if( filename = FindString( buffer, bra, 0, BufDim))
{
for( s = filename; *s ; s++);
for( r = ext ; *r; *s++ = *r++);
*s = 0;
Printf("Rename %s as %s\n", oldfilename, filename);
RenameFont( oldfilename, filename);
}
}
///
void ChangeNameTTF(STRPTR oldfilename, STRPTR buffer, LONG BufDim)
{
Printf("TTF Font\n");
}
/// STRPTR FindString( STRPTR buff, STRPTR bra, STRPTR ket, ULONG dim)
STRPTR FindString( STRPTR buff, STRPTR bra, STRPTR ket, ULONG dim)
{
/*
Return the string between bra & ket
if ket = 0 then takes the string until eol
*/
STRPTR s,r,s1,s2;
LONG i;
i = dim;
r=bra;s1=buff;
while (*r && i)
{
r=bra;
for( s = s1; *r == *s ; r++, s++);
if(*r != 0)
{
s1++;
i--;
}
else s1 = s;
}
if(i && ket) // bra found
{
r=ket;s2=s1;
while (*r && i)
{
r=ket;
for( s = s2; *r == *s ; r++, s++);
if(*r != 0)
{
s2++;
i--;
}
}
if(i) // ket found
{
*s2 = 0;
return s1;
}
} else if(ket == 0)
{
for(s=s1; (*s != 0x0a) && (*s != 0x0d) ; s++);
*s = 0;
return s1;
}
return 0;
}
///
/// void RenameFont( STRPTR from , STRPTR to)
void RenameFont( STRPTR from , STRPTR to)
{
BPTR olddir,currdir;
STRPTR s;
char c;
s = PathPart( from);
c = *s; *s= 0;
if( currdir = Lock( from, ACCESS_READ ))
{
if( olddir = CurrentDir( currdir))
{
*s = c;
s = FilePart(from);
if( !Rename( s, to)) PrintFault( IoErr(), s);
} else PrintFault(IoErr(), from);
CurrentDir( olddir);
} else PrintFault(IoErr(), from);
UnLock( currdir);
}
///